﻿using pingjia.bll;
using PJTY.dal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class views_customer_sqlb : System.Web.UI.Page
{
    private ConsumerService consumerService = new ConsumerService();

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox masterCheckBox = (CheckBox)sender;
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox detailCheckBox = (CheckBox)row.FindControl("CheckBox1");
            if (detailCheckBox != null)
            {
                detailCheckBox.Checked = masterCheckBox.Checked;
            }
        }
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // No implementation needed for this example
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        List<string> selectedIds = GetSelectedIds();
        UpdateConsumerRecords(selectedIds, consumerService.Update);
        GridView1.DataBind();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        List<string> selectedIds = GetSelectedIds();
        UpdateConsumerRecords(selectedIds, consumerService.Update1);
        GridView1.DataBind();
    }

    private List<string> GetSelectedIds()
    {
        List<string> selectedIds = new List<string>();
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox checkBox = (CheckBox)row.FindControl("CheckBox1");
            if (checkBox != null && checkBox.Checked)
            {
                selectedIds.Add(row.Cells[1].Text);
            }
        }
        return selectedIds;
    }

    private void UpdateConsumerRecords(List<string> ids, Action<string> updateMethod)
    {
        foreach (string id in ids)
        {
            try
            {
                updateMethod(id);
            }
            catch (Exception ex)
            {
                // Handle exception (log it, show a message to the user, etc.)
                // For example: LogError(ex);
            }
        }
    }
}